<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include_once('../service/productService.php');
include_once("../service/categoryService.php");
include_once '../service/imageService.php';

$page_num = $_GET['page_num'];
if (!isset($page_num)) {
    $page_num = 1;
}
$row_per_page = 16;

$cateID = $_GET['cate'];

if (!isset($cateID)) {
    $cateID = "";
}

$status = $_GET['status'];

if (!isset($status)) {
    $status = "";
}

$total = getNumberOfProductByCateAndStatus($status, $cateID);
$total_page = ceil($total / $row_per_page);
$start_position = ($page_num * $row_per_page) - ($row_per_page);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Sản Phẩm</title>
        <link href="../css/main.css" type="text/css" rel="stylesheet"/>
    </head>

    <body>
        <div class="wrapper">
            <?php include("header.php"); ?>
            <div class="main-body">
                <?php include("left_menu.php"); ?>
                <div class="main-body-right">
                    <div class="top-thumbnail thin-margin">
                        <img src="../img/layout/DRESSES Collection.jpg" width="731" height="313"/>
                    </div>

                    <div class="paging">
                        <div class="left">
                            <?php
                            if ($cateID != "") {
                                $thisCate = getCategoryByID($cateID);
                                echo($thisCate['CAT_NAME']);
                            } else if ($status != "") {
                                $statusArray = array(1 => 'Còn Hàng', 2 => 'Hàng Mới Về', 3 => 'Hết Hàng', 4 => 'Hàng Sắp Về', 5 => 'Sale off', 6 => 'Ẩn');
                                echo($statusArray[$status]);
                            }
                            ?>
                        </div>
                        <div class="right">
                            <?php
                            for ($i = 1; $i <= $total_page; $i++) {
                                $param = "";
                                if ($cateID == "") {
                                    $param = "product.php?status=" . $status;
                                } else {
                                    $param = "product.php?cate=" . $cateID;
                                }
                                $param .= "&page_num=" . $i;

                                if ($i == $page_num) {
                                    $class = "class='paging-text current'";
                                } else {
                                    $class = "class='paging-text'";
                                }
                                echo("<a " . $class . "href='" . $param . "'>" . $i . "</a>");
                            }
                            ?>
                        </div>
                    </div>
                    <div class="middle-body">
                        <?php
                        $products = getProductsByCateAndStatus($status, $cateID, $start_position, $row_per_page);
                        while ($product = mysql_fetch_array($products)) {
                            $image = getMainImageByProID($product['PRO_ID']);
                            if ($image['IMG_PATH'] == '') {
                                $thumbnailPath = "../img/layout/no-image.png";
                            } else {
                                $thumbnailPath = "../img/product/thumbnail/" . $image['IMG_PATH'];
                            }
                            ?>
                            <div class="product-container">
                                <div>
                                    <?php
                                    if ($product['PRO_STATUS'] == 2) {
                                        echo'<img src="../img/layout/new-Button.png" width="50" height="48" style="position: absolute;"/>';
                                    } else if ($product['PRO_STATUS'] == 5) {
                                        echo'<img src="../img/layout/sale.png" width="50" height="48" style="position: absolute;"/>';
                                    }
                                    ?>
                                    <a href="detail.php?id=<?php echo($product['PRO_ID']); ?>"><img src="<?php echo($thumbnailPath); ?>" width="178"  height="222"/></a>
                                </div>
                                <div class="product-text"><a href="detail.php?id=<?php echo($product['PRO_ID']); ?>"><?php echo($product['PRO_NAME']); ?></a></div>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
            </div>
            <?php include("footer.php"); ?>
        </div>
    </body>
</html>
